from django.db import models
import uuid
from .dimensionModel import Dimension
from .prefixModel import UnitPrefix
from control.models.User import User


class Unit(models.Model):
    uuid = models.UUIDField(primary_key=True, auto_created=True,
                            default=uuid.uuid4, editable=False, verbose_name="uuid")
    unit_name = models.CharField(
        max_length=200, verbose_name="单位名称", null=False, blank=True, default="")
    unit_name_en = models.CharField(
        max_length=200, verbose_name="单位英文名称", null=False, blank=True, default="")
    sei_id = models.CharField(
        max_length=200, verbose_name="单位编码", null=False, blank=True, default="")
    unit_symbol = models.CharField(
        max_length=256, null=False, blank=False, verbose_name="单位符号")
    prefix = models.ForeignKey(
        UnitPrefix, verbose_name="单位词头", on_delete=models.DO_NOTHING, null=True, blank=True)
    dimension = models.ForeignKey(
        Dimension, verbose_name="量纲", on_delete=models.DO_NOTHING, null=False, blank=False)
    is_show = models.BooleanField(default=True, verbose_name="是否显示")
    root_unit = models.ForeignKey(
        'self', verbose_name="换算单位", on_delete=models.DO_NOTHING, null=True, blank=True, related_name="export_unit")
    conversion_factor = models.FloatField(
        null=True, blank=True, verbose_name="单位换算系数")
    conversion_offset = models.FloatField(
        null=True, blank=True, verbose_name="单位换算偏移数")
    unit_description = models.TextField(
        verbose_name='单位描述', null=False, blank=True, default="")
    unit_description_en = models.TextField(
        verbose_name='单位英文描述', null=False, blank=True, default="")
    unit_comment = models.TextField(
        verbose_name='单位备注', null=False, blank=True, default="")
    create_person = models.ForeignKey(
        User, verbose_name="创建人", related_name="create_unit", on_delete=models.DO_NOTHING, null=False)
    create_date = models.DateTimeField(verbose_name='创建时间', auto_now_add=True)
    modify_person = models.ForeignKey(
        User, verbose_name="修改人", related_name="modify_unit", on_delete=models.DO_NOTHING, null=False)
    modify_date = models.DateTimeField(verbose_name='修改时间', auto_now=True)
    valid_flag = models.BooleanField(default=True, verbose_name="是否有效")

    class Meta:
        ordering = ['create_date']
        verbose_name = "Unit"
        verbose_name_plural = "Units"

        constraints = [
            models.UniqueConstraint(fields=['unit_name'], condition=models.Q(
                valid_flag=True) & ~models.Q(unit_name=''), name='unique_valid_unit_name'),
            models.UniqueConstraint(fields=['unit_name_en'], condition=models.Q(
                valid_flag=True)& ~models.Q(unit_name_en=''), name='unique_valid_unit_name_en'),
            models.UniqueConstraint(fields=['sei_id'], condition=models.Q(valid_flag=True) & ~models.Q(
                sei_id=''), name='unique_valid_unit_sei_id'),
        ]

    def __str__(self):
        return "单位" + "->" + self.unit_name + " : " + self.unit_symbol

    def show_full_name(self):
        if self.unit_name != "" and self.unit_name_en != "":
            return "{}({})".format(self.unit_name, self.unit_name_en)
        if self.unit_name != "":
            return self.unit_name
        if self.unit_name_en != "":
            return self.unit_name_en
        return "未命名单位"

    def show_short_name(self):
        if self.unit_name != "":
            return self.unit_name
        if self.unit_name_en != "":
            return self.unit_name_en
        return "未命名单位"
    
    def show_name_symbol(self):
        if self.unit_name != "":
            return self.unit_name + "(" + self.unit_symbol + ")"
        if self.unit_name_en != "":
            return self.unit_name_en + "(" + self.unit_symbol + ")"
        return "未命名单位" + "(" + self.unit_symbol + ")"
